package com.prdiot.poetry.controller;

import com.prdiot.poetry.domains.Poems;
import com.prdiot.poetry.service.ChengyuService;
import com.prdiot.poetry.service.MingjuService;
import com.prdiot.poetry.service.PoemsService;
import com.prdiot.poetry.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
public class IndexController {

    @Autowired
    PoemsService poemsService;

    @Autowired
    TagsService tagsService;

    @Autowired
    MingjuService mingjuService;

    @Autowired
    ChengyuService chengyuService;

    @Autowired
    private TemplateEngine templateEngine;

    /*
     * 首页 推荐
     */
    @GetMapping("/")
    public String index0(Model model, String page){

        int curPage = 1;
        try{
            curPage = Integer.valueOf(page) + 1;
        }catch (Exception exception){
            curPage = new Random().nextInt(999999);
        }

        List<Poems> list = poemsService.getRandPoetry();

        model.addAttribute("poemsList", list);
        model.addAttribute("page", curPage);
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("themeList",mingjuService.getMingjuTheme());
        model.addAttribute("mingjuList",mingjuService.getRandMingju());
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        return "index";
    }

    @GetMapping("/baidu/sitemap/{type}/{page}")
    public void baiduSiteMap(HttpServletResponse response,@PathVariable(name = "type") String type,
                             @PathVariable(name = "page") Integer page) throws IOException {
        //生成txt格式 sitemap 文件
        //type可选择 poems mingju chengyu author
        Writer writer = response.getWriter();
        String lineSeparator = System.getProperty("line.separator", "\n");
        writer.append("User-agent: *").append(lineSeparator).append("Allow:/");
        writer.flush();
        writer.close();
    }

    //错误捕捉后 统一重定向到首页
    @GetMapping("/errorPage/{code}")
    public String errorPage(@PathVariable(name = "code") Integer code, Model model){

        List<Poems> list = poemsService.getRandPoetry();

        model.addAttribute("poemsList", list);
        model.addAttribute("page", 1);
        model.addAttribute("tagList",tagsService.getTagsOrderByPoemCount(100));
        model.addAttribute("themeList",mingjuService.getMingjuTheme());
        model.addAttribute("chengyuList",chengyuService.getRandPoetry());
        return "index";
    }

    //获取译注赏内容
    @GetMapping("/getPoemExt/{type}/{poemid}")
    @ResponseBody
    public String getPoemExt(@PathVariable(name = "type") String type, @PathVariable(name = "poemid") String poemid){
        if (StringUtils.isEmpty(type) || StringUtils.isEmpty(poemid)){
            return "none";
        }else{
            Map<String, Object> poemExt =  poemsService.getPoetryExt(poemid);

            Context context = new Context();
            context.setVariable("body", poemExt.get("body"));

            if (type.contains("yi")){
                context.setVariable("trans", poemExt.get("trans"));
                context.setVariable("transRel", poemExt.get("transRel"));
            }
            if (type.contains("zhu")){
                context.setVariable("expl", poemExt.get("expl"));
            }
            if (type.contains("shang")){
                context.setVariable("shang", poemExt.get("shang"));
            }
            String result = templateEngine.process("poemExt", context);
            return result;
        }
    }

    @GetMapping("/mianze")
    public String mianze(Model model, String page){
        return "mianze";
    }
}
